package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.MonitorProduct;
import com.banksoft.cps.bean.MonitorValue;
import com.banksoft.cps.bean.OffsetOrder;
import com.banksoft.cps.bean.SaleOrder;
import com.banksoft.cps.bean.query.MonitorProductQuery;
import com.banksoft.cps.bean.query.OffsetOrderQuery;
import com.banksoft.cps.bean.warpper.OffsetOrderWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.MonitorValueService;
import com.banksoft.cps.service.OffsetOrderService;
import com.banksoft.cps.service.SaleOrderService;
import com.banksoft.cps.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2018/2/24.
 */
@Controller
@RequestMapping("/admin/offsetOrder/")
public class AdminOffsetOrderController extends ControllerConstants {

    @Autowired
    OffsetOrderService offsetOrderService;
    @Autowired
    SaleOrderService saleOrderService;
    @Autowired
    MonitorValueService monitorValueService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(OffsetOrderQuery query){
        List<OffsetOrderWarpper> list =  offsetOrderService.listWarpper(query);
        return toJSONString(createDataTip(list));
    }

    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(OffsetOrderQuery query){
        long count =  offsetOrderService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(OffsetOrder bean, HttpServletRequest request){
        try {
            offsetOrderService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String saleOrderId){
        OffsetOrder offsetOrder = offsetOrderService.findBySaleOrderId(saleOrderId);
        SaleOrder saleOrder = saleOrderService.findById(saleOrderId);
        MonitorValue monitorValue = monitorValueService.findBySaleOrderId(saleOrderId);
        Map<String,Object> map  = new HashMap<>();
        map.put("offsetOrder",offsetOrder);
        map.put("saleOrder",saleOrder);
        map.put("monitorValue",monitorValue);
        return toJSONString(createDataTip(map));
    }





}
